import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { PerFamilyEmploymentApi } from '#/api/per/familyEmployment';
import type { DescriptionItemSchema } from '#/components/description';

import { h } from 'vue';

import { DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },

    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        placeholder: '请选择与本人关系',
      },
    },

    {
      fieldName: 'familyName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        placeholder: '请输入身份证号',
      },
    },

    {
      fieldName: 'employerName',
      label: '单位名称',
      component: 'Input',
      componentProps: {
        placeholder: '请输入单位名称',
      },
    },
    {
      fieldName: 'employerType',
      label: '单位性质',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_EMPLOYER_TYPE),
        placeholder: '请选择单位性质',
      },
    },
    {
      fieldName: 'positionName',
      label: '职务',
      component: 'Input',
      componentProps: {
        placeholder: '请输入职务',
      },
    },

    {
      fieldName: 'isSeniorPosition',
      label: '是否担任高级职务',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'isLiveTogether',
      label: '是否共同生活',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },
    {
      fieldName: 'isEmployed',
      label: '是否在职',
      component: 'RadioGroup',
      defaultValue: false,
      componentProps: {
        options: [
          { label: '是', value: true },
          { label: '否', value: false },
        ],
        buttonStyle: 'solid',
        optionType: 'button',
      },
    },

    {
      fieldName: 'remarks',
      label: '备注',
      component: 'Textarea',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 详情页字段 */
export function useDetailSchema(): DescriptionItemSchema[] {
  return [
    {
      label: '人员ID',
      field: 'employeeId',
    },
    {
      label: '与本人关系',
      field: 'relation',
    },
    {
      label: '系统人员ID',
      field: 'systemPersonId',
    },
    {
      label: '姓名',
      field: 'familyName',
    },
    {
      label: '证件类型',
      field: 'cardTypeName',
    },
    {
      label: '身份证号',
      field: 'familyIdcard',
    },
    {
      label: '单位名称',
      field: 'employerName',
    },
    {
      label: '单位性质',
      field: 'employerTypeName',
    },
    {
      label: '职务',
      field: 'positionName',
    },
    {
      label: '是否担任高级职务',
      field: 'isSeniorPositionName',
    },
    {
      label: '是否共同生活',
      field: 'isLiveTogetherName',
    },
    {
      label: '是否在职',
      field: 'isEmployedName',
    },
    {
      label: '状态',
      field: 'statusName',
    },
    {
      label: '备注',
      field: 'remarks',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
    {
      label: '其他参数',
      field: 'metadata',
      render: (val) =>
        h('div', {
          innerHTML: val,
          class: 'whitespace-pre-wrap text-sm leading-relaxed',
        }),
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'relation',
      label: '与本人关系',
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.PER_RELATION),
        allowClear: true,
        placeholder: '请输入与本人关系',
      },
    },

    {
      fieldName: 'familyName',
      label: '姓名',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入姓名',
      },
    },
    {
      fieldName: 'cardType',
      label: '证件类型',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.SYSTEM_CARD_TYPE),
        placeholder: '请选择证件类型',
      },
    },
    {
      fieldName: 'familyIdcard',
      label: '身份证号',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入身份证号',
      },
    },

    {
      fieldName: 'employerName',
      label: '单位名称',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入单位名称',
      },
    },

    {
      fieldName: 'employerType',
      label: '单位性质',
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: getDictOptions(DICT_TYPE.PER_EMPLOYER_TYPE),
        placeholder: '请选择单位性质',
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<PerFamilyEmploymentApi.FamilyEmployment>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '主键ID',
      minWidth: 120,
      visible: false,
    },

    {
      field: 'relation',
      title: '与本人关系',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_RELATION },
      },
    },

    {
      field: 'familyName',
      title: '姓名',
      minWidth: 120,
    },
    {
      field: 'cardType',
      title: '证件类型',
      minWidth: 100,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.SYSTEM_CARD_TYPE },
      },
    },
    {
      field: 'familyIdcard',
      title: '身份证号',
      minWidth: 120,
    },
    {
      field: 'employerName',
      title: '单位名称',
      minWidth: 120,
    },
    {
      field: 'employerType',
      title: '单位性质',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.PER_EMPLOYER_TYPE },
      },
    },
    {
      field: 'positionName',
      title: '职务',
      minWidth: 120,
    },
    {
      field: 'isSeniorPosition',
      title: '是否担任高级职务',
      minWidth: 80,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'isLiveTogether',
      title: '是否共同生活',
      minWidth: 80,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'isEmployed',
      title: '是否在职',
      minWidth: 80,
      cellRender: {
        name: 'CellDictBadge',
      },
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },

    {
      field: 'actions',
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
